package com.ccjq.shosestore.order.service.impl;

import com.ccjq.shosestore.core.domain.PageInfo;
import com.ccjq.shosestore.core.utils.ArithUtils;
import com.ccjq.shosestore.core.utils.PageUtils;
import com.ccjq.shosestore.core.utils.UuidUtils;
import com.ccjq.shosestore.order.dao.OrderDao;
import com.ccjq.shosestore.order.dao.impl.OrderDaoImpl;
import com.ccjq.shosestore.order.po.CarAndProductPo;
import com.ccjq.shosestore.order.po.Order;
import com.ccjq.shosestore.order.po.OrderPo;
import com.ccjq.shosestore.order.service.OrderService;

import java.util.ArrayList;
import java.util.List;

public class OrderServiceImpl implements OrderService {
    OrderDao orderDao = new OrderDaoImpl();
    public int insertXinXi(OrderPo orderPo) {
        int result = orderDao.insertXinXi(orderPo);
        return result;
    }

    public int insertGuanLian(String uuid,String user_id) {
        int result = 0;
        List<CarAndProductPo> listCar = orderDao.productCarList(user_id);
        for(CarAndProductPo cpp:listCar){
            Order order = new Order();
            order.setOrder_id(uuid);
            order.setProduct_id(cpp.getProduct_id());
            order.setQuantity(cpp.getQuantity());
            order.setPrice(orderDao.selectPrice(cpp.getProduct_id()));
            result = orderDao.insertGuanLian(order);
            int quan = orderDao.selectStockQuantity(cpp.getProduct_id());
            if(quan>cpp.getQuantity()){
                orderDao.updateStock(cpp.getQuantity(),cpp.getProduct_id());
                System.out.println("购物车id+"+cpp.getShopping_cart_id());
            }else {
                break;
            }
        }
        return result;
    }

    public int deleteCar(String user_id) {
        int result = orderDao.deleteCar(user_id);
        return result;
    }

    public int updateStock(int quantity, String product_id) {
        int result = orderDao.updateStock(quantity,product_id);
        return result;
    }

    public List<OrderPo> selectAllOrder() {
        List<OrderPo> list = orderDao.selectAllOrder();
        return list;
    }

    public OrderPo selectOrder(String order_id) {
        OrderPo orderPo = orderDao.selectOrder(order_id);
        return orderPo;
    }

    public List<CarAndProductPo> productCarList(String user_id) {
        List<CarAndProductPo> list = orderDao.productCarList(user_id);
        return list;
    }

    public List<CarAndProductPo> selectCarQuantity(String shopping_cart_id, String user_id) {
        List<CarAndProductPo> list = orderDao.selectCarQuantity(shopping_cart_id,user_id);
        return null;
    }

    public int selectStockQuantity(String product_id) {
        int result = orderDao.selectStockQuantity(product_id);
        return result;
    }

    public String uuid() {
        String orderUuid = UuidUtils.generateShortUUID("D", 10);
        return orderUuid;
    }

    public PageInfo<OrderPo> selectFengYeAll(String user_id,int pageSize, int pageNo) {
        int startRow = PageUtils.getStartRow(pageSize, pageNo);
        int pageSizes = (pageSize+startRow)-1;
        System.out.println("起始行="+startRow);
        List<OrderPo> listAll = orderDao.selectFengYeAll(user_id,startRow,pageSizes);
        System.out.println("分页集合长度="+listAll.size());
        int total = orderDao.pageCount(user_id);
        System.out.println("总行数="+total);
        int pages = PageUtils.getPageCount(total, pageSize);
        System.out.println("总页数="+pages);
        PageInfo<OrderPo> pageInfo = new PageInfo<OrderPo>();
        pageInfo.setPageNo(pageNo);
        pageInfo.setPageSize(pageSize);
        pageInfo.setPages(pages);
        pageInfo.setTotal(total);
        pageInfo.setObjs(listAll);
        return pageInfo;
    }

    public List<CarAndProductPo> selectorder(String order_id) {
        List<CarAndProductPo> list= new ArrayList<CarAndProductPo>();
        List<CarAndProductPo> listOrder = orderDao.selectorder(order_id);
        for (CarAndProductPo cp:listOrder){
            CarAndProductPo orderPo = new CarAndProductPo();
            orderPo.setProduct_id(cp.getProduct_id());
            orderPo.setFeature(cp.getFeature());
            orderPo.setQuantity(cp.getQuantity());
            orderPo.setPrice(cp.getPrice());
            orderPo.setSale(ArithUtils.mul(cp.getQuantity(), cp.getPrice()));//小计
            list.add(orderPo);
        }
        return list;
    }


}
